# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
# 导入管道
from MaoMiAV3.items import ShortVideoAreaItem


class ShortVideoAreaSpider(scrapy.Spider):
    name = 'ShortVideoArea'
    custom_settings = {
        'ITEM_PIPELINES': {
            "MaoMiAV3.pipelines.ShortVideoAreaPipeline": 300
        }
    }
    allowed_domains = ['www.2d88353a06d715bf.xyz']
    start_urls = ['https://www.2d88353a06d715bf.xyz/shipin/list-短视频.html']

    def __init__(self):
        self.website_url = "https://www.2d88353a06d715bf.xyz"
        self.settings_url = "https://www.2d88353a06d715bf.xyz/shipin/list-短视频-{}.html"

    # 访问子页面
    def parse_detail(self, response):
        # 接收数据
        video_dict = response.meta["video_dict"]
        # 创建管道
        item = ShortVideoAreaItem()
        # 提取视频下载链接
        video_download_url = response.xpath("//tr[@class='app_hide']/td/input/@value").extract()[0]
        item["video_name"] = video_dict["video_name"]
        item["video_date"] = video_dict["video_date"]
        item["video_download_url"] = video_download_url
        # 返回数据
        yield item

    # 访问主页面
    def parse(self, response):
        # 对数据解码
        video_List = response.xpath("//li[@class='content-item']")
        for i in video_List:
            # 定义字典
            video_dict = {
                # 提取视频日期
                "video_date": i.xpath("./a/span[last()]/text()").extract()[0],
                # 提取视频名称
                "video_name": i.xpath(".//div//a/text()").extract()[0]
            }
            video_page_url = i.xpath(".//div//a/@href").extract()[0]
            # 拼接视频页面连接
            video_dict["video_page_url"] = self.website_url + video_page_url
            yield Request(url=video_dict["video_page_url"], callback=self.parse_detail, meta={"video_dict": video_dict})
        # 获取页数
        page_list = response.xpath("//div[@class='pagination']/a[last()-2]/text()").extract()[0]
        page_list = str(page_list).split("/")
        # 当前页数
        current_page = int(page_list[0])
        # 最后页数
        last_page = int(page_list[-1])
        # 判断当前页数是否小于或等于最后一页
        print("当前页数:", current_page)
        print("总页数:", last_page)
        if current_page < last_page:
            # 下一页
            next_page = current_page + 1
            next_page = self.settings_url.format(next_page)
            yield Request(url=next_page, callback=self.parse)
